package com.gxa.clientadmin.service.impl;

/*
 *
 *@author leijun
 *
 *@creating 2020/11/13 16:50
 *
 */

import com.gxa.clientadmin.exception.BusinessException;
import com.gxa.clientadmin.mapper.AdminMapper;
import com.gxa.clientadmin.mapper.AssignMapper;
import com.gxa.clientadmin.mapper.PermissionMaper;
import com.gxa.clientadmin.pojo.Admin;
import com.gxa.clientadmin.pojo.Assign;
import com.gxa.clientadmin.pojo.Permission;
import com.gxa.clientadmin.service.AdminService;
import com.gxa.clientadmin.utils.MD5Util;
import com.gxa.clientadmin.utils.Salt;
import com.gxa.clientadmin.utils.TreeUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName AdminServiceImpl
 * @Description TODO
 * @Author 俊
 * @Date 2020/11/13 16:50
 * @Version 1.0
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;

    @Resource
    AssignMapper assignMapper;

    @Resource
    PermissionMaper permissionMaper;

    @Override
    public Integer add(Admin admin) {
        admin.setCreate(new Date());
        String salt = Salt.createSalt();
        admin.setPassword(MD5Util.encode(admin.getPassword()+salt));
        admin.setSalt(salt);
        return adminMapper.insertSelective(admin);
    }

    @Override
    public Integer delete(Integer id) {
        return adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer update(Integer id, String password) {
        Admin admin = new Admin();
        admin.setId(id);
        Admin byId = adminMapper.selectOne(admin);
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        admin.setPassword(MD5Util.encode(password+byId.getSalt()));
        return adminMapper.updateByExampleSelective(admin,example);
    }

    @Override
    public Map list() {
        HashMap<String, Object> map = new HashMap<>();
        List<Admin> admins = adminMapper.selectAll();
        map.put("list",admins);
        return map;
    }

    @Override
    public Integer updatePermission(Map map) {
        int x=0;
        List<Integer> rights= (List<Integer>) map.get("rights");
        Admin id = adminMapper.selectByPrimaryKey(map.get("id"));
        if (id==null){
            return 0;
        }
        for (int i=0;i<rights.size();i++){
            Assign assign = new Assign();
            assign.setAId((Integer) map.get("id"));
            assign.setPId(rights.get(i));
            assignMapper.insertSelective(assign);
            x++;
        }
        if ( x==rights.size()){
            return 1;
        }
        return 0;
    }

    @Override
    public Map login(String username, String password) {
        //创建一个hashmap存放返回的数据
        HashMap<String , Object> map = new HashMap<>();
        //根据管理员输入的username查询管理员的所有信息
        Example example3 = new Example(Admin.class);
        Example.Criteria criteria3 = example3.createCriteria();
        criteria3.andEqualTo("username",username);
        Admin adminByKey = adminMapper.selectOneByExample(example3);
        //判断输入的管理员是否为空，为空则返回管理员名或密码错误
        if (adminByKey==null){
            throw new BusinessException("管理员名或密码错误");
        }
        //创建一个Example对象
        Example example = new Example(Admin.class);
        //创建一个createCriteria对象
        Example.Criteria criteria = example.createCriteria();
        //在criteria对象中输入查询的条件管理员名和密码
        criteria.andEqualTo("username",username);
        criteria.andEqualTo("password",MD5Util.encode(password+adminByKey.getSalt()));//根据管理员名得到admin对象得到管理员的盐，然后与管理员输入的密码加密比较
        //将管理员名和密码放入查询
        List<Admin> admins = adminMapper.selectByExample(example);
        //如果查询不到管理员信息则说明管理员名或密码错误
        if (admins==null){
            throw new BusinessException("管理员名或密码错误");
        }
        //根据管理员的id查询管理员权限
        Example example1 = new Example(Assign.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("aId",adminByKey.getId());
        List<Assign> assigns = assignMapper.selectByExample(example1);
        //创建一个list列表用于存放permission数据
        List<Permission> list=new LinkedList<>();
        HashMap<String, Permission> map1 = new HashMap<>();
        //循环遍历管理员对应的权限信息
       for (int i=0;i<assigns.size();i++){
           //根据权限id查询对应的权限信息
           Example example2 = new Example(Permission.class);
           Example.Criteria criteria2 = example2.createCriteria();
           criteria2.andEqualTo("menuid",assigns.get(i).getPId());
           List<Permission> permissions = permissionMaper.selectByExample(example2);
           for (Permission permission : permissions) {
               //将查询到的权限信息写入list
               list.add(permission);
           }

       }

        List<Permission> treeList = TreeUtils.getTreeList(list);
        //将查询到的信息写入map中
        map.put("admin",admins);
        map.put("permission",treeList);

        //生成管理员的登录时间,写入管理员表
        Example example4 = new Example(Admin.class);
        Example.Criteria criteria4 = example4.createCriteria();
        criteria4.andEqualTo("username",username);
        Admin admin = new Admin();
        admin.setLoginDate(new Date());
        adminMapper.updateByExampleSelective(admin,example4);
        return map;
    }

    @Override
    public Admin selectOne(String username) {
        //根据管理员输入的username查询管理员的所有信息
        Example example3 = new Example(Admin.class);
        Example.Criteria criteria3 = example3.createCriteria();
        criteria3.andEqualTo("username",username);
        Admin adminByKey = adminMapper.selectOneByExample(example3);
        return adminByKey;
    }


}
